package com.sd.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class OnMotionEventActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.testmain);

		final TextView text2 = (TextView) findViewById(R.id.text2);
		final CharSequence edit = text2.getText();
		text2.setCursorVisible(true);

		text2.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int sta = 0;
				int end = 0;

				event.getX();
				event.getY();

				switch (event.getAction()) {

				case MotionEvent.ACTION_DOWN: {
					sta = text2.getSelectionStart();

					return true;
				}

				case MotionEvent.ACTION_MOVE: {
					text2.setTextColor(Color.RED);
					SpannableStringBuilder styled = new SpannableStringBuilder(
							edit);
					styled.setSpan(new ForegroundColorSpan(Color.BLUE), 1, 30,
							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					text2.setText(styled);
					return true;
				}

				case MotionEvent.ACTION_UP: {
					end = text2.getSelectionEnd();
					CharSequence sel = edit.subSequence(5, 80);
					return true;
				}
				}
				return true;
			}
		});

	}

}
